﻿Public Class frmHoaDonBanHang
    Dim ma As String
    Private Sub frmHoaDonBanHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDanhSachLenLuoi()
        LoadTenKhachHang()
    End Sub
    Public Sub LoadDanhSachLenLuoi()
        dgvDanhSachMatHang.DataSource = MatHangDAO.selectall()
    End Sub
    Public Sub loadHoaDon(ByVal mahd As String)
        dgvDanhSachChiTietPhieu.DataSource = HoaDonBanDAO.selecChiTiet(mahd)
    End Sub

    Public Sub HienThi()
        If dgvDanhSachMatHang.SelectedRows.Count > 0 Then
            txtMaMatHang.Text = dgvDanhSachMatHang.SelectedRows(0).Cells(0).Value.ToString()
            txtTenMatHang.Text = dgvDanhSachMatHang.SelectedRows(0).Cells(1).Value.ToString()
            txtSoLuongBan.Text = dgvDanhSachMatHang.SelectedRows(0).Cells(5).Value.ToString()
        End If
    End Sub

    Private Sub dgvDanhSachMatHang_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDanhSachMatHang.SelectionChanged
        HienThi()
    End Sub
    Public Sub LoadTenKhachHang()
        cmbTenKhachHang.DataSource = KhachHangDAO.selectall()
        cmbTenKhachHang.DisplayMember = "TENKHACHHANG"
        cmbTenKhachHang.ValueMember = "MAKHACHHANG"
    End Sub
    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim cthd As New ChiTietHoaDonDTO
        cthd.SoLuongBan = txtSoLuongBan.Text
        If RbDonGiaSi.Checked = True Then
            cthd.DONGIABAN = dgvDanhSachMatHang.SelectedRows(0).Cells(3).Value.ToString()
        Else
            cthd.DONGIABAN = dgvDanhSachMatHang.SelectedRows(0).Cells(4).Value.ToString()
        End If
        cthd.MaMatHang = txtMaMatHang.Text
        cthd.MaChiTietHoaDon = ChiTietHoaDonDAO.MaTuTang()
        cthd.MaHoaDonBan = txtMaPhieu.Text
        ChiTietHoaDonDAO.Insert(cthd)
        loadHoaDon(txtMaPhieu.Text)
        LoadTongGiaTriHoaDon()
        updateTongTien()
    End Sub
   
    Private Sub btnLapPhieu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLapPhieu.Click
        'insert vao bang hoa don ban
        Dim hd As New HoaDonBanDTO
        hd.MaHoaDonBan = txtMaPhieu.Text
        hd.MaKHachHang = cmbTenKhachHang.SelectedValue
        'hd.NgayLapHoaDonBan = DateTimePicker
        ' hd.TriGiaHoaDonBan = HoaDonBanDAO.TotalValue(txtMaPhieu.Text)
        LoadTongGiaTriHoaDon()
        loadHoaDon(txtMaPhieu.Text)
        HoaDonBanDAO.Insert(hd)

    End Sub
    Public Sub LoadTongGiaTriHoaDon()
        Try
            lbTongTien.Text = "Tong gia tri: " & HoaDonBanDAO.TotalValue(txtMaPhieu.Text)
        Catch ex As Exception
            lbTongTien.Text = "Tong gia tri: 0d"
        End Try
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click

        ChiTietHoaDonDAO.delete(ma)
        loadHoaDon(txtMaPhieu.Text)
        LoadTongGiaTriHoaDon()
        updateTongTien()
    End Sub

    Private Sub dgvDanhSachChiTietPhieu_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDanhSachChiTietPhieu.SelectionChanged
        If dgvDanhSachChiTietPhieu.SelectedRows.Count > 0 Then
            ma = dgvDanhSachChiTietPhieu.SelectedRows(0).Cells(0).Value.ToString()
        End If
    End Sub

    Public Sub updateTongTien()
        Dim hd As New HoaDonBanDTO
        hd.MaHoaDonBan = txtMaPhieu.Text
        hd.TriGiaHoaDonBan = HoaDonNhapDAO.TotalValue(txtMaPhieu.Text)
        HoaDonBanDAO.Update(hd)
    End Sub
End Class